Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Configure connections using DNS domain names #2047

Merged
merged 1 commit into from
Jan 23, 2025

Conversation

hessjcg
Copy link
Collaborator

@hessjcg hessjcg commented Jul 24, 2024

The dialer may be configured to use a DNS name to look up the instance
name instead of configuring the connector with the instance name directly.

Add a DNS TXT record for the Cloud SQL instance to a private DNS server
or a private Google Cloud DNS Zone used by your application. For example:

  • Record type: TXT
  • Name: prod-db.mycompany.example.com – This is the domain name used by the application
  • Value: my-project:region:my-instance – This is the instance connection name
  • Configure the dialer with the cloudsqlconn.WithDNSResolver() option.

Open a database connection using the DNS name:

HOST: The domain name configured in your DNS TXT record.

Base JDBC URL: jdbc:mysql://<HOST>/<DATABASE_NAME>

SOCKET_FACTORY_CLASS: com.google.cloud.sql.mysql.SocketFactory

The full JDBC URL for MySQL should look like this:

String jdbcUrl = "jdbc:mysql://<HOST>/<DATABASE_NAME>?" 
    + "&socketFactory=com.google.cloud.sql.mysql.SocketFactory" 
    + "&user=<MYSQL_USER_NAME>" 
    + "&password=<MYSQL_USER_PASSWORD>";

See also: the Cloud SQL Go Connector implementation

Part of #2043

@hessjcg hessjcg changed the title Gh 2043 configure with dns feat: Configure connections using DNS domain names Aug 8, 2024
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from 46e95c4 to 9d71720 Compare August 8, 2024 19:28
@hessjcg hessjcg changed the base branch from main to gh-2043-use-jdbc-url-hostname August 8, 2024 19:49
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from 9d71720 to cc206b2 Compare August 8, 2024 19:51
@hessjcg hessjcg force-pushed the gh-2043-use-jdbc-url-hostname branch from b368416 to baa2494 Compare August 13, 2024 16:43
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from cc206b2 to b57ca6c Compare August 13, 2024 16:43
@hessjcg hessjcg force-pushed the gh-2043-use-jdbc-url-hostname branch from baa2494 to b4fab59 Compare August 13, 2024 16:57
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch 2 times, most recently from 01c0a38 to 1f3d6b7 Compare August 13, 2024 18:31
@hessjcg hessjcg force-pushed the gh-2043-use-jdbc-url-hostname branch from b4fab59 to 7901caf Compare August 13, 2024 18:33
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch 2 times, most recently from 49c7e38 to f3f362e Compare August 13, 2024 18:38
@hessjcg hessjcg force-pushed the gh-2043-use-jdbc-url-hostname branch from 7901caf to c251493 Compare August 13, 2024 18:38
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from f3f362e to a0c1b4e Compare August 13, 2024 19:15
@hessjcg hessjcg force-pushed the gh-2043-use-jdbc-url-hostname branch from c251493 to a6c517d Compare August 13, 2024 19:15
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from a0c1b4e to 8aaa907 Compare August 15, 2024 17:41
@hessjcg hessjcg force-pushed the gh-2043-use-jdbc-url-hostname branch from a6c517d to 040a9d1 Compare August 15, 2024 17:41
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch 2 times, most recently from cf7698a to 7475041 Compare August 16, 2024 15:26
@hessjcg hessjcg force-pushed the gh-2043-use-jdbc-url-hostname branch from 040a9d1 to f9960d9 Compare August 16, 2024 15:26
@hessjcg hessjcg force-pushed the gh-2043-use-jdbc-url-hostname branch from f9960d9 to e8f506c Compare August 30, 2024 17:54
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from 7475041 to 8ceb527 Compare August 30, 2024 17:54
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from 8ceb527 to cf79bec Compare September 27, 2024 21:22
@hessjcg hessjcg force-pushed the gh-2043-use-jdbc-url-hostname branch 2 times, most recently from 3bd264a to fe70759 Compare October 1, 2024 16:07
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from cf79bec to 170f88f Compare October 1, 2024 16:07
@hessjcg hessjcg force-pushed the gh-2043-use-jdbc-url-hostname branch from fe70759 to b0942f4 Compare January 10, 2025 18:20
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from 170f88f to 4ad47df Compare January 10, 2025 18:20
@hessjcg hessjcg force-pushed the gh-2043-use-jdbc-url-hostname branch from b0942f4 to 6062f19 Compare January 10, 2025 18:23
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from 4ad47df to e885216 Compare January 13, 2025 18:47
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from de8f2dd to 2b909fd Compare January 17, 2025 18:44
@hessjcg hessjcg force-pushed the gh-2043-use-r2dbc-host branch from 0f1d338 to c389387 Compare January 17, 2025 18:59
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch 2 times, most recently from 475ac57 to ca711d8 Compare January 17, 2025 19:41
} else {
dns = instanceMetadata.getDnsName();
}

if (dns == null || dns.isEmpty()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String.isNullOrEmptry(dns) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

"cloudSqlInstance property not set. Please specify this property in the JDBC URL or the "
+ "connection Properties with value in form \"project:region:instance\"");
config.getCloudSqlInstance() != null || config.getDomainName() != null,
"cloudSqlInstance property and hostname not set. Please specify"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

try {
return new CloudSqlInstanceName(name);
} catch (IllegalArgumentException e) {
// Not a well-formed instance name.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shallowing exception ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use CloudSqlInstanceName.isValidInstanceName() instead.

@hessjcg hessjcg marked this pull request as ready for review January 23, 2025 16:46
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from ca711d8 to 3cd89b1 Compare January 23, 2025 16:47
@hessjcg hessjcg requested a review from pandirigoog January 23, 2025 16:55
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from 3cd89b1 to 595bfe3 Compare January 23, 2025 18:10
@hessjcg hessjcg force-pushed the gh-2043-use-r2dbc-host branch from c389387 to 69c1d42 Compare January 23, 2025 18:14
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch 2 times, most recently from e0bbb24 to 914dfdb Compare January 23, 2025 18:54
@hessjcg hessjcg force-pushed the gh-2043-use-r2dbc-host branch from 69c1d42 to 367102f Compare January 23, 2025 18:54
@hessjcg hessjcg force-pushed the gh-2043-use-r2dbc-host branch from 367102f to 3c91ade Compare January 23, 2025 19:23
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from 914dfdb to 78f1345 Compare January 23, 2025 20:56
@hessjcg hessjcg force-pushed the gh-2043-use-r2dbc-host branch 2 times, most recently from f2bba72 to 221aeac Compare January 23, 2025 21:20
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from 78f1345 to 06dec39 Compare January 23, 2025 21:20
@hessjcg hessjcg force-pushed the gh-2043-use-r2dbc-host branch from 221aeac to fe9437a Compare January 23, 2025 21:36
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from 06dec39 to a993bcd Compare January 23, 2025 21:36
@hessjcg hessjcg force-pushed the gh-2043-configure-with-dns branch from a993bcd to 93691d5 Compare January 23, 2025 22:05
@hessjcg hessjcg changed the base branch from gh-2043-use-r2dbc-host to main January 23, 2025 22:06
@hessjcg hessjcg enabled auto-merge (squash) January 23, 2025 22:06
@hessjcg hessjcg merged commit cb745f2 into main Jan 23, 2025
15 checks passed
@hessjcg hessjcg deleted the gh-2043-configure-with-dns branch January 23, 2025 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants